//
// Copyright 2001-2004 Stottler Henke Associates, Inc.
// All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////

package com.stottlerhenke.simbionic.engine.parser;

import com.stottlerhenke.simbionic.common.*;
import com.stottlerhenke.simbionic.api.*;
import com.stottlerhenke.simbionic.api.SB_Param;
import com.stottlerhenke.simbionic.engine.SB_SimInterface;
import com.stottlerhenke.simbionic.engine.SB_SingletonBook;
import com.stottlerhenke.simbionic.engine.file.SB_StreamReader;

/**
 * Represents a single variable of type entity.
 */
public class SB_VarEntity extends SB_Variable 
{

  public SB_VarEntity() {
    _parameterValue = new SB_Param( SB_SimInterface.TranslateId( SB_ID.NULL_ID()) );
  }
  
  public SB_VarEntity(SB_ID entityID){
    _parameterValue = new SB_Param( SB_SimInterface.TranslateId(entityID) );
  }
  
  public SB_VarEntity(SB_VarEntity var) throws SB_Exception
  {
    Copy(var);
  }
  
  public SB_Variable Clone() throws SB_Exception
  {
    return new SB_VarEntity(this);
  }

  public void load(SB_StreamReader reader,SB_SingletonBook book) 
		throws SB_FileException
	{
    // junk value, can't load entity ids from file
    reader.readInt();
  }

  private void Copy( SB_VarEntity var) throws SB_Exception
  {
    // don't need to deep-copy the SB_ID (they're managed by the SB_IdDispenser)
    _parameterValue = new SB_Param( SB_SimInterface.TranslateId(var.GetValue()) );
  }

  public void SetValue(SB_ID value){
    _parameterValue.setEntity(SB_SimInterface.TranslateId(value));
  }
  
  public SB_VarType GetType() { return SB_VarType.kEntity; }
  
  public void setSB_Param(SB_Param newParam) throws SB_Exception
  {
    newParam.getEntity();
    
    _parameterValue = newParam;
  }
  
  public SB_ID GetValue() throws SB_Exception
  {
    return  SB_SimInterface.TranslateId( _parameterValue.getEntity() );
  }
  
  public static SB_ID GET_ENTITY(SB_Variable var) throws SB_Exception
  {
    return ((SB_VarEntity)var).GetValue();
  }
  
  SB_Variable opAssign( SB_VarEntity var) 
  throws SB_Exception
  {
    if (this != var) {
      Copy(var);
    }
    return this;
  }
  
  public SB_Variable opOR( SB_Variable arg2)  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opAND( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opGRTEq( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opLSSEq( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opNOTEq( SB_Variable arg2)  
  throws SB_Exception
  {
    SB_Variable result = null;

    if(arg2.GetType() == SB_VarType.kEntity)
      result = new SB_VarBoolean( GetValue().opNOTEq(GET_ENTITY(arg2)) );
    else if(arg2.GetType() == SB_VarType.kInvalid)
      result = new SB_VarBoolean( true );
    return result;
  }

  public SB_Variable equals( SB_Variable arg2) throws SB_Exception  {
    SB_Variable result = null;

    if(arg2.GetType() == SB_VarType.kEntity)
      result = new SB_VarBoolean( GetValue().equals(GET_ENTITY(arg2)) );
    else if(arg2.GetType() == SB_VarType.kInvalid)
      result = new SB_VarBoolean( false );
    return result;
  }

  public SB_Variable opGRT( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opLSS( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opMOD( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opEXP( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opDIV( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opMUL( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opSUB( SB_Variable arg2) throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public SB_Variable opADD( SB_Variable arg2) throws SB_Exception  {
    SB_Variable result = null;

    if (arg2.GetType() == SB_VarType.kString)	// returns a concatenated string
    {
      String workStr;
      workStr = "ENTITY-" + GetValue()._id + SB_VarString.GET_STRING(arg2);
      result = new SB_VarString( workStr );
    }
    return result;
  }

  public SB_Variable opNOT() throws SB_Exception  {
    return new SB_VarBoolean( GetValue().equals( SB_ID.NULL_ID()) );
  }

  public SB_Variable opNEG() throws SB_Exception  {
    //FATAL( false, "Invalid operation for entity variable");
    return null;
  }

  public boolean bool() throws SB_Exception {
    return !GetValue().equals(SB_ID.NULL_ID());
  }

  public String toString()
  {
    try
    {
      return new String("kEntity: " + GetValue());
    }
    catch(SB_Exception ex)
    {
      return ex.toString();
    }
  }

}